package sword.T45;

import java.util.*;

/**
 * @Author: 18362
 * @Create: 2022-04-09 12:44:17 星期六
 */

class Solution {
    // 关键在于第20行
    public String minNumber(int[] nums) {
        String[] arr = new String[nums.length];
        for (int i=0; i< nums.length; ++i) {
            arr[i] = String.valueOf(nums[i]);
        }
        Arrays.sort(arr, (a, b) -> {
            if (a.equals(b))
                return 0;
            String ab = a + b, ba = b + a;          // 此题精髓所在！！！
            for (int i=0; i<ab.length(); ++i) {
                if (ab.charAt(i) < ba.charAt(i))
                    return -1;
                else if (ab.charAt(i) > ba.charAt(i))
                    return 1;
            }
            return 0;
        });
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<arr.length; ++i) {
            sb.append(arr[i]);
        }
        return sb.toString();
    }
}
